<?php namespace Common\Model;
use \Hdphp\Model\Model;
/*
 * 分类模型
 * */
 	class category extends Model
 	{
 		protected $table = "category";
		protected $validate = array();
		//添加
		public function addcate()
		{
			if(!$this->create()) return FALSE;
			return $this->add();
		}
		//获得除子类和自己的方法
		public function getcate($cid)
		{
			//获得所有分类
			$allcate = $this->get();
			//p($allcate);die;
			//2.找子集
			$son = $this->getSon($cid,$allcate);
			//3,将自己压入数组
			$son[] = $cid;
			//p($son);die;
			//去除
			$son = implode(',', $son);
			$cate = $this->where("cid not in ($son)")->get();
			//p($cate);die;
			
			return $cate;
		}
		//找子集
		public function getSon($cid,$allcate)
		{   static $son =array();
			
			foreach($allcate as $k=>$v){
				if($v["pid"]==$cid){
					$son[] = $v['cid']; 
					$this->getSon($v['cid'],$allcate);
				}
			}
			//p($son);die;
			return $son;
		}
		
		/***************************************
		 * 编辑
		 * ****************************************/
		public function resetCate($cid)
		{
			if(!$this->create()) return FALSE;
			return $this->where('cid',$cid)->save();
		}
		/***************************************
		 * 删除
		 * ****************************************/
		 public function del($cid)
		 {
		 	return $this->where("cid",$cid)->delete();
		 }
 	}
